#include "test_sd.h"
#include "cm_uart.h"
#include "cm_demo_uart.h"
#include "stdio.h"
#include "stdlib.h"

static int32_t sd_fopen_fd = -1;

void cm_test_sd_operation(unsigned char*cmd[],int len)
{
    if(0 == strncmp("ISFORMAT",cmd[2],strlen("ISFORMAT")))
    {
        char vol[3] = {0};
        int ret = snprintf(vol,3,"%s:",cmd[3]);
        bool sd_is_formatted = cm_sd_is_formatted(vol);
        cm_demo_printf("cm_sd_is_formatted ret:%d\n",sd_is_formatted);
        OCTEST_FUNC_EXPECT_GREATER_OR_EQUAL_INT32(0,sd_is_formatted,cm_sd_is_formatted);
    }else if(0 == strncmp("FORMAT",cmd[2],strlen("FORMAT")))
    {
        char vol[3] = {0};
        int ret = snprintf(vol,3,"%s:",cmd[3]);
        int32_t sd_format = cm_sd_format(vol);
        cm_demo_printf("cm_sd_format ret:%d\n",sd_format);
        OCTEST_FUNC_EXPECT_EQUAL_INT32(0,sd_format,cm_sd_format);
    }else if(0 == strncmp("OPEN",cmd[2],strlen("OPEN")))
    {
        char filename[512] = {0};
        char *mode = cmd[5];
        int ret_1 = snprintf(filename,512,"%s:%s",cmd[3],cmd[4]);
        sd_fopen_fd = cm_sd_fopen(filename,mode);
        cm_demo_printf("cm_sd_fopen ret:%d\n",sd_fopen_fd);
        OCTEST_FUNC_EXPECT_GREATER_OR_EQUAL_INT32(0,sd_fopen_fd,cm_sd_fopen);
    }else if(0 == strncmp("CLOSE",cmd[2],strlen("CLOSE")))
    {
        int32_t sd_fclose = cm_sd_fclose(sd_fopen_fd);
        cm_demo_printf("cm_sd_fclose ret:%d\n",sd_fclose);
        OCTEST_FUNC_EXPECT_EQUAL_INT32(0,sd_fclose,cm_sd_fclose);
    }else if(0 == strncmp("READ",cmd[2],strlen("READ")))
    {
        char buf[512] = {0};
        uint32_t size = strtoul(cmd[3],NULL,10);
        int32_t sd_fread = cm_sd_fread(sd_fopen_fd,buf,size);
        cm_demo_printf("cm_sd_fread ret:%d\n",sd_fread);
        cm_demo_printf("read data: %s\n",buf);
        OCTEST_FUNC_EXPECT_GREATER_OR_EQUAL_INT32(0,sd_fread,cm_sd_fread);
    }else if(0 == strncmp("WRITE",cmd[2],strlen("WRITE")))
    {
        uint32_t size = strtoul(cmd[4],NULL,10);
        int32_t sd_fwrite = cm_sd_fwrite(sd_fopen_fd, cmd[3],size);
        cm_demo_printf("cm_sd_fwrite ret:%d\n",sd_fwrite);
        OCTEST_FUNC_EXPECT_EQUAL_INT32(size,sd_fwrite,cm_sd_fwrite);
    }else if(0 == strncmp("SEEK",cmd[2],strlen("SEEK")))
    {
        int32_t offset = atoi(cmd[3]);
        int32_t base = -1;
        if(0 == strcmp("SD_SEEK_SET",cmd[4]))
        {
            base = CM_SD_SEEK_SET;
        }
        else if(0 == strcmp("SD_SEEK_CUR",cmd[4]))
        {
            base = CM_SD_SEEK_CUR;
        }else if(0 == strcmp("SD_SEEK_END",cmd[4]))
        {
            base = CM_SD_SEEK_END;
        }
        else {
            cm_demo_printf("param error!\n");
        }
        int32_t sd_fseek = cm_sd_fseek(sd_fopen_fd,offset,base);
        cm_demo_printf("cm_sd_fseek ret:%d\n",sd_fseek);
        OCTEST_FUNC_EXPECT_EQUAL_INT32(0,sd_fseek,cm_sd_fseek);
    }else if(0 == strncmp("DELETE",cmd[2],strlen("DELETE")))
    {
        char filename[512] = {0};
        int ret_1 = snprintf(filename,512,"%s:%s",cmd[3],cmd[4]);
        int32_t sd_fdelete = cm_sd_fdelete(filename);
        cm_demo_printf("cm_sd_fdelete ret:%d\n",sd_fdelete);
        OCTEST_FUNC_EXPECT_EQUAL_INT32(0,sd_fdelete,cm_sd_fdelete);
    }else if(0 == strncmp("MOVE",cmd[2],strlen("MOVE")))
    {
        char src[500] = {0};
        char dest[500] = {0};
        int ret_1 = snprintf(src,500,"%s:%s",cmd[3],cmd[4]);
        int ret_2 = snprintf(dest,500,"%s:%s",cmd[5],cmd[6]);
        int32_t sd_fmove = cm_sd_fmove(src,dest);
        cm_demo_printf("cm_sd_fmove ret:%d\n",sd_fmove);
        OCTEST_FUNC_EXPECT_EQUAL_INT32(0,sd_fmove,cm_sd_fmove);
    }else if(0 == strncmp("EXIST",cmd[2],strlen("EXIST")))
    {
        char filename[512] = {0};
        int ret_1 = snprintf(filename,512,"%s:%s",cmd[3],cmd[4]);
        int32_t sd_exist = cm_sd_exist(filename);
        cm_demo_printf("cm_sd_exist ret:%d\n",sd_exist);
        OCTEST_FUNC_EXPECT_EQUAL_INT32(1,sd_exist,cm_sd_exist);
    }else if(0 == strncmp("FILESIZE",cmd[2],strlen("FILESIZE")))
    {
        char filename[512] = {0};
        int ret_1 = snprintf(filename,512,"%s:%s",cmd[3],cmd[4]);
        int32_t sd_filesize = cm_sd_filesize(filename);
        cm_demo_printf("cm_sd_filesize ret:%d\n",sd_filesize);
        OCTEST_FUNC_EXPECT_GREATER_OR_EQUAL_INT32(0,sd_filesize,cm_sd_filesize);
    }else if(0 == strncmp("MKDIR",cmd[2],strlen("MKDIR")))
    {
        char path[500] = {0};
        int ret_1 = snprintf(path,500,"%s:%s",cmd[3],cmd[4]);
        int32_t sd_mkdir = cm_sd_mkdir(path);
        cm_demo_printf("cm_sd_mkdir ret:%d\n",sd_mkdir);
        OCTEST_FUNC_EXPECT_EQUAL_INT32(0,sd_mkdir,cm_sd_mkdir);
    }else if(0 == strncmp("RMDIR",cmd[2],strlen("RMDIR")))
    {
        char path[500] = {0};
        int ret_1 = snprintf(path,500,"%s:%s",cmd[3],cmd[4]);
        int32_t sd_rmdir = cm_sd_rmdir(path);
        cm_demo_printf("cm_sd_rmdir ret:%d\n",sd_rmdir);
        OCTEST_FUNC_EXPECT_EQUAL_INT32(0,sd_rmdir,cm_sd_rmdir);
    }else if(0 == strncmp("SET_CURDIR",cmd[2],strlen("SET_CURDIR")))
    {
        char path[500] = {0};
        int ret_1 = snprintf(path,500,"%s:%s",cmd[3],cmd[4]);
        int32_t sd_set_currentdir = cm_sd_set_currentdir(path);
        cm_demo_printf("cm_sd_set_currentdir ret:%d\n",sd_set_currentdir);
        OCTEST_FUNC_EXPECT_EQUAL_INT32(0,sd_set_currentdir,cm_sd_set_currentdir);
    }else if(0 == strncmp("GET_CURDIR",cmd[2],strlen("GET_CURDIR")))
    {
        char path[500] = {0};
        int32_t sd_get_currentdir = cm_sd_get_currentdir(path, 500);
        cm_demo_printf("cm_sd_get_currentdir ret:%d\n",sd_get_currentdir);
        cm_demo_printf("currentdir:%s\n",path);
        OCTEST_FUNC_EXPECT_EQUAL_INT32(0,sd_get_currentdir,cm_sd_get_currentdir);
    }else if(0 == strncmp("GET_INFO",cmd[2],strlen("GET_INFO")))
    {
        char vol[3] = {0};
        int ret = snprintf(vol,3,"%s:",cmd[3]);
        cm_sd_system_info_t info = {0};
        int32_t sd_getinfo = cm_sd_getinfo(vol, &info);
        cm_demo_printf("cm_sd_getinfo ret:%d\n",sd_getinfo);
        cm_demo_printf("total_size:%lld, free_size:%lld\n",info.total_size,info.free_size);
        OCTEST_FUNC_EXPECT_EQUAL_INT32(0,sd_getinfo,cm_sd_getinfo);
    }else if(0 == strncmp("GET_FLAG",cmd[2],strlen("GET_FLAG")))
    {
        int32_t sd_get_flag = cm_sd_get_flag();
        cm_demo_printf("cm_sd_get_flag ret:%d\n",sd_get_flag);
        OCTEST_FUNC_EXPECT_GREATER_OR_EQUAL_INT32(0,sd_get_flag,cm_sd_get_flag);
    }
    else if(0 == strncmp("SET_FLAG",cmd[2],strlen("SET_FLAG")))
    {
        int32_t flag = atoi(cmd[3]);
        int32_t sd_set_flag = cm_sd_set_flag(flag);
        cm_demo_printf("cm_sd_set_flag ret:%d\n",sd_set_flag);
        OCTEST_FUNC_EXPECT_EQUAL_INT32(0,sd_set_flag,cm_sd_set_flag);
    }
    else{
        cm_demo_printf("param error!\n");
    }
}